package com.dai.zhenfujin.my.activity.wallet;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;

import com.dai.zhenfujin.Constants.Constants;
import com.dai.zhenfujin.R;
import com.dai.zhenfujin.base.BaseActivity;
import com.dai.zhenfujin.bean.UserBean;
import com.dai.zhenfujin.bean.WXPayBean;
import com.dai.zhenfujin.http.CallBack;
import com.dai.zhenfujin.http.HttpClient;
import com.dai.zhenfujin.utils.UIUtils;
import com.tencent.mm.opensdk.modelpay.PayReq;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import java.util.HashMap;
import java.util.Map;

/**
 * 零钱 充值页面
 */
public class TopUpActivity extends BaseActivity implements View.OnClickListener {

    //    private TextView tvWorkingRange ,tvCompany ,tvPosition;
//    private ImageView ivHeadPortrait;
    private RadioButton rb1, rb2, rb3, rb4, rb5, rb6;
    //
    private Button button;
    //    private CheckBox checkBox;
    private int position;


    private TextView tvTitle, tvAccount;
    private ImageView imageView;

    private TextView tvPrice;
    private UserBean userBean;
    public  IWXAPI mWxApi;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_top_up);

        initView();
        initListener();
        mWxApi = WXAPIFactory.createWXAPI(this, Constants.WECHAT_ID, false);
        mWxApi.registerApp(Constants.WECHAT_ID);

//        if (checkBox.isChecked()){
//            checkBox.setChecked(true);
//        }else {
//            checkBox.setChecked(false);
//        }
    }

    private void initListener() {

        rb1.setOnClickListener(this);
        rb2.setOnClickListener(this);
        rb3.setOnClickListener(this);
        rb4.setOnClickListener(this);
        rb5.setOnClickListener(this);
        rb6.setOnClickListener(this);

        button.setOnClickListener(this);

        imageView.setOnClickListener(this);
        tvAccount.setOnClickListener(this);

    }

    private void initView() {
//        tvWorkingRange = findViewById(R.id.head_working_range);//工作范围
//        tvCompany = findViewById(R.id.head_company);//公司
//        tvPosition = findViewById(R.id.head_position);//运营总监
//        ivHeadPortrait = findViewById(R.id.head_head_portrait);
        userBean = (UserBean) getIntent().getSerializableExtra("userBean");

        button = findViewById(R.id.top_up_button);//去支付
//        checkBox = findViewById(R.id.top_up_checkbox);

        tvPrice = (TextView) findViewById(R.id.top_up_price);//价钱

        tvTitle = findViewById(R.id.title_textview);
        tvTitle.setText("零钱");
        tvAccount = findViewById(R.id.title_tv);
        tvAccount.setVisibility(View.VISIBLE);
        tvAccount.setText("账单");
        imageView = findViewById(R.id.title_imageview);
        if (null != userBean) {
            tvPrice.setText("￥" + UIUtils.getamount(userBean.user_balance)+ "元");
        }else {
            tvPrice.setText("￥" + 300.00 + "元");
        }


        rb1 = findViewById(R.id.top_up_rb1);
        rb2 = findViewById(R.id.top_up_rb2);
        rb3 = findViewById(R.id.top_up_rb3);
        rb4 = findViewById(R.id.top_up_rb4);
        rb5 = findViewById(R.id.top_up_rb5);
        rb6 = findViewById(R.id.top_up_rb6);

        rb1.setChecked(true);


    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.title_imageview:
                finish();
                break;
            case R.id.title_tv:
                Intent intent = new Intent(this, AccountActivity.class);
                startActivity(intent);
                break;
            case R.id.top_up_button:
                String qian = null;
//                checkBox.isChecked();//获取选中状态
                if (position == 0) {
                    qian = rb1.getText().toString();
                } else if (position == 1) {
                    qian = rb2.getText().toString();
                } else if (position == 2) {
                    qian = rb3.getText().toString();
                } else if (position == 3) {
                    qian = rb4.getText().toString();
                } else if (position == 4) {
                    qian = rb5.getText().toString();
                } else if (position == 5) {
                    qian = rb6.getText().toString();
                }
                getWXPaySign(qian.split("元")[0]);
               // Toast.makeText(this, "选中的钱数" + qian, Toast.LENGTH_LONG).show();
                break;
            case R.id.top_up_rb1:
                position = 0;
                rb1.setChecked(true);
                rb2.setChecked(false);
                rb3.setChecked(false);
                rb4.setChecked(false);
                rb5.setChecked(false);
                rb6.setChecked(false);
                break;
            case R.id.top_up_rb2:
                position = 1;
                rb1.setChecked(false);
                rb2.setChecked(true);
                rb3.setChecked(false);
                rb4.setChecked(false);
                rb5.setChecked(false);
                rb6.setChecked(false);
                break;
            case R.id.top_up_rb3:
                position = 2;
                rb1.setChecked(false);
                rb2.setChecked(false);
                rb3.setChecked(true);
                rb4.setChecked(false);
                rb5.setChecked(false);
                rb6.setChecked(false);
                break;
            case R.id.top_up_rb4:
                position = 3;
                rb1.setChecked(false);
                rb2.setChecked(false);
                rb3.setChecked(false);
                rb4.setChecked(true);
                rb5.setChecked(false);
                rb6.setChecked(false);
                break;
            case R.id.top_up_rb5:
                position = 4;
                rb1.setChecked(false);
                rb2.setChecked(false);
                rb3.setChecked(false);
                rb4.setChecked(false);
                rb5.setChecked(true);
                rb6.setChecked(false);
                break;
            case R.id.top_up_rb6:
                position = 5;
                rb1.setChecked(false);
                rb2.setChecked(false);
                rb3.setChecked(false);
                rb4.setChecked(false);
                rb5.setChecked(false);
                rb6.setChecked(true);
                break;
        }
    }

    private void getWXPaySign(String qian) {

        String url = Constants.WXPAY_SIGN;

        Map<String, Object> params = new HashMap<>();
        Map<String, String> params2 = new HashMap<>();

        params2.put("pay_type", "2");
        params2.put("trade_money", Integer.valueOf(qian) + "");
        params.put("req_bd", params2);

        HttpClient.post(this, url, params, new CallBack<WXPayBean>() {
            @Override
            public void onSuccess(WXPayBean wayPay) {
                PayReq req = new PayReq();
                req.appId = wayPay.getPay_data().getAppid();
                req.partnerId = wayPay.getPay_data().getPartnerid();
                req.prepayId = wayPay.getPay_data().getPrepayid();
                req.nonceStr = wayPay.getPay_data().getNonce_str();
                req.timeStamp = wayPay.getPay_data().getTimestamp();
                req.packageValue = wayPay.getPay_data().getPackageX();
                req.sign = wayPay.getPay_data().getSign();
                req.extData = "0.02";
                mWxApi.sendReq(req);
            }
        });
    }
}
